<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>父子组件传值2</title>
    <script src="../lib/vue_v2.6.14.js"></script>
  </head>
  <body>
    <div id="app">
      <my-cpn :fruits="fruits" :title="title"></my-cpn>
    </div>

    <template id="cpn">
      <div>
        <h2>水果列表</h2>
        <ul>
          <li v-for="(item, index) in fruits" :key="index">{{ item }}</li>
        </ul>

        <p>必传值：{{ title }}</p>

        <h3>默认值</h3>
        <ul>
          <li v-for="(item, index) in movies" :key="index">{{ item }}</li>
        </ul>
      </div>
    </template>

    <script>
      // 子组件
      Vue.component('my-cpn', {
        template: '#cpn',
        props: {
          // 1、只匹配类型
          fruits: Array,

          // 2、必传，否则会报错
          title: {
            type: String,
            required: true
          },

          // 3、默认值。注意：vue2.5.3 版本以上，对象或数组默认值必须从一个函数中获取
          movies: {
            type: Array,
            default() {
              return ['Vue', 'React', 'Angular']
            }
          }
        }
      })

      // vue实例充当父组件
      const vm = new Vue({
        el: '#app',
        data: {
          fruits: ['苹果', '梨子', '香蕉'],
          title: '越努力越幸运',
          movies: ['速度与激情', '拆弹专家', '银河补习班', '敢死队']
        }
      })
    </script>
  </body>
</html>
